package com.youmingxing.wxapi;

import android.app.Activity;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.Const;

import org.json.JSONObject;

/**
 * Created by lang on 16/1/3.
 */
public class UMManger {

    private Activity context;
    public UMSocialService mController;
    private static UMManger manger;

    private static final int TYPE_QQ = 3;
    private static final int TYPE_WEIXIN = 1;
    private static final int TYPE_CIRCLE = 2;
    private static final int TYPE_WEIBO = 4;

    private String timestamp;//服务器返回的时间戳,分享成功后发回去
    int entrance;//来源页面
    /*
     entrance为1时，传用户ID；
     entrance为2时，传 海选ID；
     entrance为3时，传 发布的动态ID
 */
    private int id;//用户id,海选id,或作品id
    private int type;//qq,weixin,weibo....

    public static UMManger getInstance() {
        if (manger == null) {
            manger = new UMManger();
        }
        return manger;
    }


    public void getInstance(Activity context) {
        //设置新浪SSO handler
        this.context = context;
        mController = UMServiceFactory.getUMSocialService("com.umeng.share");
        mController.getConfig().setSsoHandler(new SinaSsoHandler());
        //qq
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(context, Const.APPID_QQ,
                Const.APP_KEY_QQ);
        qqSsoHandler.addToSocialSDK();
        //wx
        UMWXHandler wxHandler = new UMWXHandler(context, Const.APPID_WEIXIN, Const.SECRET_WEIXIN);
        wxHandler.addToSocialSDK();
        //wx friend circle
        UMWXHandler wxCircleHandler = new UMWXHandler(context, Const.APPID_WEIXIN, Const.SECRET_WEIXIN);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();
    }

    public void shareContent(final int id, final int entrance, final int type, final SHARE_MEDIA share_media) {
        this.id = id;
        this.type = type;
        this.entrance = entrance;
        SelfInfoApi.getShareContent(type, id, entrance, new BaseJsonHandler(context) {
            @Override
            public void onSuccessWithData(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                timestamp = data.optString("_r");
                switch (type) {
                    case TYPE_QQ:
                        QQShareContent qqShareContent = new QQShareContent();
                        qqShareContent.setShareContent(data.optString("title"));
                        qqShareContent.setShareImage(new UMImage(context, data.optString
                                ("img")));
                        qqShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(qqShareContent);
                        mController.postShare(context, share_media, mShareListener);
                        break;
                    case TYPE_WEIXIN:
                        WeiXinShareContent weiXinShareContent = new WeiXinShareContent();
                        weiXinShareContent.setShareContent(data.optString("title"));
                        weiXinShareContent.setShareImage(new UMImage(context, data.optString
                                ("img")));
                        weiXinShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(weiXinShareContent);
                        mController.postShare(context, share_media, mShareListener);
                        break;
                    case TYPE_CIRCLE:
                        CircleShareContent circleShareContent = new CircleShareContent();
                        circleShareContent.setShareContent(data.optString("title"));
                        circleShareContent.setTitle(data.optString("title"));
                        circleShareContent.setShareImage(new UMImage(context, data.optString
                                ("img")));
                        circleShareContent.setTargetUrl(data.optString("link"));
                        mController.setShareMedia(circleShareContent);
                        mController.postShare(context, share_media, mShareListener);
                        break;
                    case TYPE_WEIBO:
                        SinaShareContent sinaShareContent = new SinaShareContent();
                        sinaShareContent.setShareContent(data.optString("title") + data.optString
                                ("link"));
                        sinaShareContent.setShareImage(new UMImage(context, data.optString
                                ("img")));
                        mController.setShareMedia(sinaShareContent);
                        mController.postShare(context, share_media, mShareListener);
                        break;
                }
            }
        });
    }


    /**
     * 分享监听器
     */
    SocializeListeners.SnsPostListener mShareListener = new SocializeListeners.SnsPostListener() {

        @Override
        public void onStart() {
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int stCode,
                               SocializeEntity entity) {
            if (stCode == 200) {
                Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT)
                        .show();
                SelfInfoApi.shareSuccessCallback(type, id, entrance, timestamp, new
                        BaseJsonHandler(context) {
                            @Override
                            public void onSuccessWithData(JSONObject response) {
                                context.finish();
                            }
                        });

            } else {

            }
        }
    };


}
